package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.entity.DoctorSchedule;
import com.example.hospital.service.DoctorScheduleService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/doctor-schedules")
public class DoctorScheduleController {
    private final DoctorScheduleService scheduleService;
    public DoctorScheduleController(DoctorScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    /**
     * 创建医生排班
     * @param s
     * @return
     */
    @PostMapping
    public ApiResponse<DoctorSchedule> create(@RequestBody DoctorSchedule s) {
        return ApiResponse.success(scheduleService.create(s)); }
    /**
     * 更新医生排班
     * @param id
     * @param s
     * @return
     */
    @PutMapping("/{id}")
    public ApiResponse<Boolean> update(@PathVariable("id") Long id, @RequestBody DoctorSchedule s) {
        s.setScheduleId(id);
        return ApiResponse.success(scheduleService.update(s));
    }
    /**
     * 删除医生排班
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> delete(@PathVariable("id") Long id) {
         return ApiResponse.success(scheduleService.delete(id));
    }
    /**
     * 分页查询医生排班（按医生ID、排班日期筛选）
     * @param doctorId
     * @param date
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public ApiResponse<PageResponse<DoctorSchedule>> list(@RequestParam(name = "doctorId", required = false) Long doctorId,
                                                          @RequestParam(name = "date", required = false) String date,
                                                          @RequestParam(name = "page", defaultValue = "1") int page,
                                                          @RequestParam(name = "size", defaultValue = "10") int size) {
        LocalDate d = (date == null || date.isEmpty()) ? null : LocalDate.parse(date);
        return ApiResponse.success(scheduleService.list(doctorId, d, page, size));
    }
}